package com.github.niefy.modules.wx.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.niefy.common.utils.R;
import com.github.niefy.modules.wx.entity.WxUser;
import com.github.niefy.modules.wx.enums.TemplateEnum;
import com.github.niefy.modules.wx.service.TemplateMsgService;
import com.github.niefy.modules.wx.service.WxUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author yucl
 * @create 2021/6/29 11:01
 */
@RestController
@RequestMapping("/template/api/")
@Api(tags = "微信模板模块")
public class MsgTemplateController {

    @Autowired
    private WxUserService wxUserService;

    @Autowired
    private TemplateMsgService templateMsgService;


    /**
     * 美康医卫士app
     */
    private static final String APPID = "wx7eeac5736e7fb5ba";

    @ApiOperation(value = "vap模块")
    @CrossOrigin
    @GetMapping("vap")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "businessNamePhone", value = "业务员手机号码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "orderSn", value = "订单编号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "patientName", value = "病人名字", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "hospitalName", value = "医院名字", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "patientNamePhone", value = "病人手机号码", required = true, dataType = "String")
    })
    public R sendVapTemplateMsg(
                                @RequestParam("businessNamePhone") String businessNamePhone,
                                @RequestParam("orderSn") String orderSn,
                                @RequestParam("patientName") String patientName,
                                @RequestParam("hospitalName") String hospitalName,
                                @RequestParam("patientNamePhone") String patientNamePhone){

        QueryWrapper<WxUser> wxUserQueryWrapper = new QueryWrapper<>();
        wxUserQueryWrapper.eq("phone",businessNamePhone);
        wxUserQueryWrapper.eq("appid",APPID);
        List<WxUser> wxUsers =  wxUserService.list(wxUserQueryWrapper);
        if(wxUsers == null || wxUsers.isEmpty()){
            return R.error("业务员手机未绑定，请先去微信公众号中绑定");
        }

        WxUser wxUser = wxUsers.get(0);

        List<WxMpTemplateData> data  = new ArrayList<>();
        data.add(new WxMpTemplateData("first","VAP报告提醒:"+wxUser.getNickname()+"业务员"));
        data.add(new WxMpTemplateData("keyword1",orderSn));
        data.add(new WxMpTemplateData("keyword2",patientName));
        data.add(new WxMpTemplateData("keyword3","vap血脂"));
        String remark = hospitalName+"医院,患者："+patientName+"， 手机号："+patientNamePhone+"的VAP血脂报告已出，请跟进，谢谢";
        data.add(new WxMpTemplateData("remark",remark));

        WxMpTemplateMessage wxMpTemplateMessage = WxMpTemplateMessage.builder()
                .templateId(TemplateEnum.VAP.getName())
                .url("")
                .toUser(wxUser.getOpenid())
                .data(data)
                .build();


        templateMsgService.sendTemplateMsg(wxMpTemplateMessage,APPID);

        return R.ok();

    }

    @ApiOperation(value = "耗材消息通知模块")
    @CrossOrigin
    @GetMapping("customer/wechat")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "businessNamePhone", value = "业务员手机号码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "name", value = "医院名字", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "orderSn", value = "订单号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "remark", value = "备注", required = true, dataType = "String")
    })
    public R sendCustomerWechatTemplateMsg(
            @RequestParam("businessNamePhone") String businessNamePhone,
            @RequestParam("name") String name,
            @RequestParam("orderSn") String orderSn,
            @RequestParam("remark") String remark){

        QueryWrapper<WxUser> wxUserQueryWrapper = new QueryWrapper<>();
        wxUserQueryWrapper.eq("phone",businessNamePhone);
        wxUserQueryWrapper.eq("appid",APPID);
        List<WxUser> wxUsers =  wxUserService.list(wxUserQueryWrapper);
        if(wxUsers == null || wxUsers.isEmpty()){
            return R.error("业务员手机未绑定，请先去微信公众号中绑定");
        }

        WxUser wxUser = wxUsers.get(0);

        List<WxMpTemplateData> data  = new ArrayList<>();
        data.add(new WxMpTemplateData("first","您有一个耗材订单未送货"));
        data.add(new WxMpTemplateData("keyword1",orderSn));
        data.add(new WxMpTemplateData("keyword2",name));
        data.add(new WxMpTemplateData("keyword3","1"));
        data.add(new WxMpTemplateData("remark",remark));

        WxMpTemplateMessage wxMpTemplateMessage = WxMpTemplateMessage.builder()
                .templateId(TemplateEnum.CUSTOMER_WECHAT.getName())
                .url("")
                .toUser(wxUser.getOpenid())
                .data(data)
                .build();


        templateMsgService.sendTemplateMsg(wxMpTemplateMessage,APPID);

        return R.ok();

    }

}
